<?php

namespace app\common\model\goods;

use app\common\model\goods\category\Region;
use think\Model;


class Category extends Model
{





    // 表名
    protected $name = 'goods_category';

    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'integer';

    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = 'updatetime';
    protected $deleteTime = false;

    // 追加属性
    protected $append = [
        'status_text',
        'region_count',
    ];


    protected static function init()
    {
        self::afterInsert(function ($row) {
            $pk = $row->getPk();
            $row->getQuery()->where($pk, $row[$pk])->update(['weigh' => $row[$pk]]);
        });
    }
    public function child()
    {
        return $this->hasMany(Category::class, 'pid', 'id');
    }
    public function  getRegionCountAttr($v, $d)
    {
        return Region::where('category_id', $d['id'])->count();
    }
    public function region()
    {
        return $this->hasMany(Region::class, 'category_id', 'id');
    }
    public function getStatusList()
    {
        return ['30' => __('Status 30')];
    }
    public function goods()
    {
        return $this->hasMany(Goods::class, 'two_cate', 'id');
    }

    public function getStatusTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['status']) ? $data['status'] : '');
        $list = $this->getStatusList();
        return isset($list[$value]) ? $list[$value] : '';
    }
}
